package stud.aistar.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import stud.aistar.entity.*;
import stud.aistar.service.IDoctorLoginService;
import stud.aistar.service.IDoctorService;
import stud.aistar.service.ISecondDepartmentService;
import stud.aistar.util.TokenUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * *
 *
 * @ClassName :wangyu
 * @Description:医生端登录service
 * @Author
 * @Date2019/9/29 20:14
 * @Version V1.0
 */
@Controller
public class DoctorLoginController {

    @Autowired
    private IDoctorLoginService loginService;

    @Autowired
    private IDoctorService doctorService;

    @Autowired
    private ISecondDepartmentService departmentService;

    @PostMapping("/login")
    @ResponseBody
    public Object login(HttpServletRequest req, HttpServletResponse rep, HttpSession session, Model model){

        String workNum = req.getParameter("workNum");
        String workPwd = req.getParameter("workPwd");

        String token = TokenUtil.generateToken(workNum);

        Map<String,Object> map = new HashMap<>();

        Doctorlogin doctorlogin = loginService.findByWorknum(workNum);

        if (doctorlogin.getWorknum()!=null&& workPwd.equals(doctorlogin.getWorkpwd())){

            req.getSession().setAttribute("doctorlogin",doctorlogin);

            map.put("token",token);
            map.put("doctorlogin",doctorlogin);
            return map;
        }

        return null;
    }

    @GetMapping("/loginview")
    @ResponseBody
    public ResultList doctorView(HttpServletRequest request){

        String doctorId = request.getParameter("doctorId");

        Doctorlogin doctorlogin = (Doctorlogin) request.getSession().getAttribute("doctorlogin");

        Doctor doctor = doctorService.findById(Integer.valueOf(doctorId));

        SecondDepartment secondDepartment = departmentService.findById(doctor.getSdeptId());

        List<Object> list = new ArrayList();

        list.add(doctor);

        list.add(secondDepartment);

        return new ResultList(CodeStatus.OK,true,"数据返回成功",list);
    }


}
